package com.lmpop.trade.verification;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.trade.sms.SendMail;
import com.lmpop.trade.ti_personal.Ti_personalInfo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class VerificationInfo
{
  QueryList queryList;

  public VerificationInfo()
  {
    this.queryList = new QueryList();
  }

  public List getVerificationInfo(SysMap mapInfo)
  {
    return this.queryList.getList("getVerificationInfo", mapInfo);
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_personalInfo Ti_personalInfo = new Ti_personalInfo();
    SendMail sendMail = new SendMail();
    Verification verification = new Verification();
    Verification verifi = new Verification();
    verification.setVerification_id(map.getString("verification_id"));
    verification.setVerification_type(map.getString("verification_type"));
    verification.setVerification_state(map.getString("verification_state"));
    verification.setVerification_target(map.getString("verification_target"));
    verification.setVerification_user(map.getString("verification_user"));
    verification.setVerification_time(map.getString("verification_time"));
    verification.setVerification_remark(map.getString("verification_remark"));

    verifi.setVerification_user(map.getString("verification_user"));
    List list = getVerificationOnlyOne(verifi);
    String result_code = "0";
    try {
      if ((list == null) || (list.size() < 1)) {
        insert(dbmMgr, verification);
      } else {
        Hashtable vmap = (Hashtable)list.get(0);
        if (!map.getString("verification_target").equals(vmap.get("verification_target").toString()))
          updatetarget(dbmMgr, map);
        else {
          updatetime(dbmMgr, map);
        }
      }
      if (map.getString("verification_type").equals("2")) {
        List plist = Ti_personalInfo.getListByPk(map.getString("verification_user").toString());
        Hashtable pmap = new Hashtable();
        if ((plist != null) && (plist.size() > 0))
          pmap = (Hashtable)plist.get(0);
        Map emap = new Hashtable();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime = tempDate.format(new Date());
        emap.put("username", pmap.get("cust_name"));
        emap.put("datetime", datetime);
        emap.put("url", map.getString("verification_id"));
        sendMail.EmailToVerification(map.getString("verification_target"), emap);
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }

    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Verification verification)
    throws ApplicationException
  {
    dbmMgr.update("insertVerificationInfo", verification);
  }

  public SysMap updatetime(DbmInterf dbmMgr, SysMap map)
  {
    Verification verification = new Verification();
    verification.setVerification_id(map.getString("verification_id"));

    String result_code = "0";
    try {
      updatetime(dbmMgr, verification);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatetime(DbmInterf dbmMgr, Verification verification) throws ApplicationException {
    dbmMgr.update("updatetime", verification);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Verification verification = new Verification();
    verification.setVerification_id(map.getString("verification_id"));
    verification.setVerification_state(map.getString("verification_state"));

    String result_code = "0";
    try {
      update(dbmMgr, verification);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Verification verification)
    throws ApplicationException
  {
    dbmMgr.update("updateVerificationState", verification);
  }

  public SysMap updatetarget(DbmInterf dbmMgr, SysMap map)
  {
    Verification verification = new Verification();
    verification.setVerification_id(map.getString("verification_id"));
    verification.setVerification_type(map.getString("verification_type"));
    verification.setVerification_state(map.getString("verification_state"));
    verification.setVerification_target(map.getString("verification_target"));
    verification.setVerification_user(map.getString("verification_user"));
    verification.setVerification_time(map.getString("verification_time"));
    verification.setVerification_remark(map.getString("verification_remark"));

    String result_code = "0";
    try {
      updatetarget(dbmMgr, verification);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatetarget(DbmInterf dbmMgr, Verification verification) throws ApplicationException {
    dbmMgr.update("updatetarget", verification);
  }

  public List getVerificationOnlyOne(Verification verification) {
    List list = this.queryList.getList("getVerificationOnlyOne", verification);
    return list;
  }

  public List getVerificationByPK(String id) {
    List list = this.queryList.getList("getVerificationByPK", id);
    return list;
  }
}